package com.sl.mes.app.service.mm;

import com.sl.mes.app.dao.mm.AggMoVo;
import com.sl.mes.db.itf.mm.IMmMo;
import com.sl.mes.db.vo.MmMo;
import com.sl.mes.db.vo.MmPmo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MoService {

    @Autowired
    private IMmMo mmMoService;

    /**
     * 根据id进行查询
     * @param cpmohid
     * @return
     */
    public AggMoVo getAggMoVoById(String cpmohid) {
        //查询主表
        MmPmo mmPmo = mmMoService.findParentVoById(cpmohid);
        List<MmMo> lst = mmMoService.findChildrenVoById(cpmohid);
        AggMoVo aggMoVo = new AggMoVo();
        aggMoVo.setMoHeadVo(mmPmo);
        aggMoVo.setMoBodyVo(lst);
        return aggMoVo;
    }

    /**
     * 根据流程订单号进行查询
     * @param vbillcode
     * @return
     */
    public AggMoVo  getAggMoVoByVbillcode(String vbillcode) {
        //查询主表
        MmPmo mmPmo = mmMoService.findParentVoByVbillcode(vbillcode);
        List<MmMo> lst = mmMoService.findChildrenVoByVbillcode(vbillcode);
        AggMoVo aggMoVo = new AggMoVo();
        aggMoVo.setMoHeadVo(mmPmo);
        aggMoVo.setMoBodyVo(lst);
        return aggMoVo;
    }

    /**
     * 添加生产订单
     *
     * @param aggMoVo
     */
    public int addMmPmo(AggMoVo aggMoVo) {
        MmPmo mmPmo = aggMoVo.getMoHeadVo();
        List<MmMo> mmMoList = aggMoVo.getMoBodyVo();
        int res = mmMoService.addMmPom(mmPmo, mmMoList);
        if (res > 0) {
            mmPmo=mmMoService.findParentVoById(mmPmo.getCpmohid());
            mmMoList=mmMoService.findChildrenVoById(mmPmo.getCpmohid());
            aggMoVo.setMoHeadVo(mmPmo);
            aggMoVo.setMoBodyVo(mmMoList);
        }
        return res;
    }

    /**
     * 删除订单表
     *
     * @param cpmohid
     */
    public int deleteMmPmo(String cpmohid) {
        int res = 0;
        try {
            res = mmMoService.deleteMmPomById(cpmohid);
            mmMoService.deleteMmMoById(cpmohid);
        } catch (Exception e) {
            res = 0;
        }
        return 0;
    }

    /**
     * 修改生产订单
     *
     * @param aggUpateVo
     * @param aggNewVo
     * @param aggDelVo
     */
    public boolean updataMmPmo(AggMoVo aggUpateVo, AggMoVo aggNewVo, AggMoVo aggDelVo) {

        boolean res = true;
        try {
            if (aggUpateVo != null) {
                mmMoService.updateMmPmo(aggUpateVo.getMoHeadVo());
                List<MmMo> mmMoList = aggUpateVo.getMoBodyVo();
                for (int i = 0; i < mmMoList.size(); i++) {
                    MmMo mmMo = mmMoList.get(i);
                    mmMo.setTs(LocalDateTime.now());
                    mmMoService.updateMmMo(mmMo);
                }
            }
            if (aggNewVo != null) {
                List<MmMo> mmMoList = aggNewVo.getMoBodyVo();
                for (int i = 0; i < mmMoList.size(); i++) {
                    MmMo mmMo = mmMoList.get(i);
                    mmMo.setTs(LocalDateTime.now());
                    mmMoService.addMmMo(mmMo);
                }
            }
            if (aggDelVo != null) {
                List<MmMo> mmMoList = aggDelVo.getMoBodyVo();
                for (int i = 0; i < mmMoList.size(); i++) {
                    MmMo mmMo = mmMoList.get(i);
                    mmMo.setTs(LocalDateTime.now());
                    mmMoService.deleteMmMoById(mmMo.getCmoid());
                }
            }
        } catch (Exception e) {
            res = false;
        }
        return res;
    }
}
